<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
// import resize from './mixins/resize'

export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  computed: {
    list() {
      //dissatisfy基本满意 normal不满意数量  satisfaction满意数量
      return this.$store.state.hcem.OneData
    }
  },
  watch: {
    list: {
      deep: true,
      handler: function () {
        this.options()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.options()
    },
    options() {
      this.chart.setOption({
        tooltip: {
          trigger: "item"
        },

        legend: {
          top: "5%",
          left: "center"
        },
        series: [
          {
            name: "",
            type: "pie",
            radius: ["35%", "50%"], // 空心饼图内外径
            center: ["50%", "55%"], // 饼图左右上下位置    靠这里
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2
            },
            // label: {
            //   show: true,
            //   position: "center"
            // },
            //           emphasis: {
            //             label: {
            //               show: true,
            //               formatter: function(data) {
            //                 return `${data.name} ${data.value}
            // (${data.percent.toFixed(1)}%)`;
            //               }
            //             }
            //           },

            labelLine: {
              show: true
            },
            data: [
              // { value: this.list.satisfaction, name: "满意" },
              // { value: this.list.dissatisfy, name: "基本满意" },
              // { value: this.list.normal, name: "不满意" }
              { value: this.list.satisfaction, name: "满意" },
              { value: this.list.normal, name: "基本满意" },
              { value: this.list.dissatisfy, name: "不满意" }
            ],
            label: {
              formatter: function (data) {
                // console.log(data);
                return `${data.value} (${data.percent !== undefined ? data.percent : 0}%)`;
              }
            }
          }
        ]
      });
    }
  }
};
</script>
